package gb.demo.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import gb.demo.common.R;
import gb.demo.entity.*;
import gb.demo.mapper.MovieMapper;
import gb.demo.mapper.UserDescripMapper;
import gb.demo.service.*;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Controller
@MapperScan("gb.demo.mapper")
public class LoveController {
    @Autowired
    private LoveService loveService;
    @Autowired
    private MovieService movieService;
    @Autowired
    private MovieLabelService movieLabelService;
    @Autowired
    private LabelService labelService;

    @Autowired
    private MovieMapper movieMapper;
    @Autowired
    private UserDescripService userDescripService;
    @Autowired
    private UserDescripMapper userDescripMapper;

    //    测试
    @RequestMapping("/LoveTest")
    @ResponseBody
    public String labelTest() {
        List<Love> list = loveService.list();
        list.stream().forEach(System.out::println);
        return JSON.toJSONString(list);
    }

    //收藏夹 完成
    @RequestMapping("/user/myfavorite")
    @ResponseBody
    public R myfavorite(HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        List<movieDetail> movieDetailList = new ArrayList<>();
        System.out.println(user);
        if (user != null) {
            QueryWrapper<Love> loveQueryWrapper = new QueryWrapper<>();
            loveQueryWrapper.eq("userid", user.getUserid());
            List<Love> listlove = loveService.list(loveQueryWrapper);
            for (Love i : listlove) {
                QueryWrapper<Movie> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("movieid", i.getMovieid());
                Movie movie = movieService.getOne(queryWrapper);
                QueryWrapper<Movielabel> movielabelQueryWrapperqueryWrapper = new QueryWrapper<>();
                movielabelQueryWrapperqueryWrapper.eq("movieid", movie.getMovieid());
                List<Movielabel> movielabelList = movieLabelService.list(movielabelQueryWrapperqueryWrapper);
                List<Label> labelList = new ArrayList<>();
                for (Movielabel ml : movielabelList) {
                    QueryWrapper<Label> labelQueryWrapper = new QueryWrapper<>();
                    labelQueryWrapper.eq("labelid", ml.getLabelid());
                    labelList.add(labelService.getOne(labelQueryWrapper));
                }
                movieDetailList.add(new movieDetail(movie, labelList));
            }
            return R.success(movieDetailList);
        } else return R.error("请登陆后重试");
    }

    //   收藏夹管理
    @RequestMapping("/user/myfavorite/delete")
    @ResponseBody
    public R favoriteDelete(String movieid, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        if (user != null) {
            QueryWrapper<Love> loveQueryWrapper = new QueryWrapper<>();
            loveQueryWrapper.eq("userid", user.getUserid()).eq("movieid", movieid);
            if (loveService.remove(loveQueryWrapper)) {
                List<Label> list = movieMapper.findMovieLabel(movieid);
                for (Label label : list) {
                    QueryWrapper<Userdescrip> userdescripQueryWrapper = new QueryWrapper<>();
                    userdescripQueryWrapper.eq("userid", user.getUserid()).eq("labelid", label.getLabelid());
                    Userdescrip userdescrip = userDescripService.getOne(userdescripQueryWrapper);
                    if (userdescrip.getWeight() <= 10) {
                        userDescripService.remove(userdescripQueryWrapper);
                    } else userDescripMapper.weightDown(user.getUserid(), label.getLabelid());
                }
                return R.success("删除成功！");
            } else return R.error("删除失败");
        } else return R.error("请登陆后重试");
    }

    //    检查是否在收藏夹
    @RequestMapping("/user/myfavorite/isLiked")
    @ResponseBody
    public R isLiked(String movieid, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        QueryWrapper<Love> loveQueryWrapper = new QueryWrapper<>();
        int userid = (user == null) ? -1 : user.getUserid();
        loveQueryWrapper.eq("userid", userid).eq("movieid", movieid);
        if (loveService.getOne(loveQueryWrapper) != null)
            return R.success("已收藏");
        return R.error("未收藏");
    }

    //    收藏特定电影
    @RequestMapping("/user/myfavorite/insert")
    @ResponseBody
    public R favoriteInsert(int movieid, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        if (user != null) {
            Love love = new Love();
            love.setUserid(user.getUserid());
            love.setMovieid(movieid);
            if (loveService.save(love)) {
                List<Label> labelList = movieMapper.findMovieLabel(String.valueOf(movieid));
                for (Label label : labelList) {
                    QueryWrapper<Userdescrip> userdescripQueryWrapper = new QueryWrapper<>();
                    userdescripQueryWrapper.eq("userid", user.getUserid()).eq("labelid", label.getLabelid());
                    Userdescrip userdescrip = userDescripService.getOne(userdescripQueryWrapper);
                    if (userdescrip == null) {
                        Userdescrip userdescripTemp = new Userdescrip();
                        userdescripTemp.setUserid(user.getUserid());
                        userdescripTemp.setLabelid(label.getLabelid());
                        userdescripTemp.setWeight(10);
                        userDescripService.save(userdescripTemp);
                    } else userDescripMapper.weightAdd(user.getUserid(), label.getLabelid());
                }
                return R.success("收藏成功");
            }
            return R.error("未收藏");
        } else return R.error("请登陆后重试");
    }

    //    甲方让加的点赞
    @RequestMapping("/user/myfavorite/dianzan")
    @ResponseBody
    public R like(int movieid, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        if (user != null) {
            List<Label> labelList = movieMapper.findMovieLabel(String.valueOf(movieid));
            for (Label label : labelList) {
                QueryWrapper<Userdescrip> userdescripQueryWrapper = new QueryWrapper<>();
                userdescripQueryWrapper.eq("userid", user.getUserid()).eq("labelid", label.getLabelid());
                Userdescrip userdescrip = userDescripService.getOne(userdescripQueryWrapper);
                if (userdescrip == null) {
                    Userdescrip userdescripTemp = new Userdescrip();
                    userdescripTemp.setUserid(user.getUserid());
                    userdescripTemp.setLabelid(label.getLabelid());
                    userdescripTemp.setWeight(1);
                    userDescripService.save(userdescripTemp);
                } else userDescripMapper.like(user.getUserid(), label.getLabelid());
            }
            return R.success("针不戳");
        } else return R.error("请登陆后重试");
    }
}


